<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResumeInfo extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'mobile' => $this->mobile,
            'avatar_url' => $this->defaultAvatar(),
            'name' => $this->name,
            'self_description' => $this->self_description??'',
            'education' => $this->educationInfo($this->education),
            'work_experience' => $this->workExperienceInfo($this->workExperience),

        ];
    }

    /**
     * 教育经历
     * @param mixed $education
     * @return array|string
     */
    private function educationInfo(mixed $education)
    {
        if (!$education) return (object)[];
        return [
            'school_name' => $education->school_name??'',
            'degree' => $education->degree??'',
            'graduation_date' => $education->graduation_date??'',
            'major' => $education->major??'',
        ];
    }

    /**
     * 工作经历
     * @param mixed $workExperience
     * @return array
     */
    private function workExperienceInfo(mixed $workExperience): array
    {
        $array = [];
        foreach ($workExperience as $value) {
            $array[] =  [
                'id'=>$value->id,
                'company_name' => $value->company_name,
                'start_date' => date("Y.m.d",strtotime($value->start_date)),
                'end_date' => date("Y.m.d",strtotime($value->end_date)),
                'job_description' => $value->job_description,
            ];
        }
        return $array;
    }
}
